-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add optional logger wherever possible #3622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This commit introduces an optional logger parameter to various structs. This enhancement allows users to provide custom logging implementations.
An issue will remain for direct calls to internal.Logger.Printf, as the call depth cannot be adjusted there without changing the function signature.
03ddd12 to
fa95d65
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ The following Jit checks failed to run:
- secret-detection-trufflehog
- static-code-analysis-semgrep-pro
#jit_bypass_commit in this PR to bypass, Jit Admin privileges required.
More info in the Jit platform.
4e23d33 to
93fa6c4
Compare
93fa6c4 to
c963331
Compare
|
I apparently failed to keep your GPG signature @ndyakov, it sounds logic as I used history rewritting to fix an issue in one of the commit I did earlier. Feel free to pull, sign your commits, and push again. My fork is now all set for maintainers edits |
| // Logger: logging.NewLoggerWrapper(logger), | ||
| // }) | ||
| // | ||
| // This will NOT handle all logging at the moment, singe there is still a global logger in use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo spotted
| // This will NOT handle all logging at the moment, singe there is still a global logger in use. | |
| // This will NOT handle all logging at the moment, since there is still a global logger in use. |
| Infof(ctx context.Context, format string, v ...any) | ||
| Debugf(ctx context.Context, format string, v ...any) | ||
| Enabled(ctx context.Context, level LogLevelT) bool | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is something I don't get here with this interface
Either LegacyLogger and LoggerWrapper also support
Error(ctx context.Context, msg string, v ...any)
Warn(ctx context.Context, msg string, v ...any)
Info(ctx context.Context, msg string, v ...any)
Debug(ctx context.Context, msg string, v ...any)Why do you restrict the interface to simili printf logger ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those were the only one used, i don't mind adding the non format methods. feel free to add them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I feel like we should consider inverting the logic here.
The custom logger available in the various config should only mention
Error(ctx context.Context, msg string, v ...any)
Warn(ctx context.Context, msg string, v ...any)
Info(ctx context.Context, msg string, v ...any)
Debug(ctx context.Context, msg string, v ...any)
Enabled(ctx context.Context, level LogLevelT) boolSo no Whateverf involved, then the code use the wrapper to transform things.
I feel like asking people to implement Errorf, Infof and co is an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of those logger methods is to print a formatted log message, so *f makes sense to me and it is consistent. Users don't have to implement those methods, they do however have to wrap their logger with the provided wrapper if their logger follows the *Context interface like slog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't suggesting to rename things such as:
Infof -> Info
My problem was the fact people will have to add a struct that would satisfy the legacy Printf methods
While we could add this in a way the logger only have to satistify the method that would be used by a structured logger
So the InfoContext one and so on.
The code could adapt itself to use the LoggerWrapper if needed.
Said otherwise people could provide directly a slog.Handler with the v9 as the logger of each config.
The logger wrapper is something we need right now because the code needs the Infof
But I would prefer the magic to be hidden from user. People seeing the interface would think "oh I need to implement Errorf, Warnf and co it's boring and not convenient"
I will add a commit to explain what I have in mind. But later, either this weekend or next week. I will also add tests for all that.
ndyakov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good to me, can we just make sure this one is addressed (preferably add a simple test):
#3618 (comment)
Here the difference I applied to an old commit. func (cl *LoggerWrapper) Errorf(ctx context.Context, format string, v ...any) {
if cl == nil || cl.logger == nil {
legacyLoggerWithLevel.Errorf(ctx, format, v...)
return
}
- cl.logger.ErrorContext(ctx, format, v...)
+ cl.logger.ErrorContext(cl.printfToStructured(ctx, format, v...))
}I can add a test, but adding a test about a logger is never fun. Maybe it can be done in a separate PR. I'm suggesting that, but if you are OK to wait for me to add the tests a few more days, maybe a week. I can do it. |
|
@ccoVeille No problem, I will prepare a beta without the logger, you can write tests and resolve conflicts in the next week and then we will include it. |
This commit introduces an optional logger parameter to various structs.
This enhancement allows users to provide custom logging implementations.
Fixes #3558
Note
This is a follow-up of #3560 that was merged in a branch (not
master) and #3618 that was updated by @ndyakov to add changes on it.The MR is now ready to be merged.